/*
 * Man.h
 *
 *  Created on: 22.05.2011
 *      Author: 123
 */

#ifndef MAN_H_
#define MAN_H_
/*
 * Man.h
 *
 *  Created on: 22.05.2011
 *      Author: 123
 */

#include <oslib/oslib.h>
#include "Camera.h"
#include "Map.h"
class sMan
{
	sMap *a;
public:
	bool LightOn;
	bool left;
	bool right;
	int x, y;
	int DrawX, DrawY;
	int fallamount;

	bool EnablePickaxe;

	OSL_IMAGE *Image[2];
	OSL_IMAGE* Torch;
	int TorchTimer;
	int TorchUpdate;
	OSL_IMAGE *Pickaxe[2];
	sMan()
	{
		TorchTimer=0;
		TorchUpdate=1;
		fallamount = 0;
		EnablePickaxe = false;
		LightOn = true;
		x = 0;
		y = 128;
		left = false;
		right = true;
	}
	inline void SetMap(sMap *a)
	{
		this->a = a;
	}
	void Fall()
	{
		if (!a->GetAbility(x, y + 64))
		{
			y += 8;
			fallamount++;
		}
		else
			fallamount = 0;
	}
	void Load()
	{
		Image[0] =
				oslLoadImageFile((char*) "data/Game/miner3_l.png", OSL_IN_RAM,
						OSL_PF_8888);
		Image[1] =
				oslLoadImageFile((char*) "data/Game/miner3_r.png", OSL_IN_RAM,
						OSL_PF_8888);
		Pickaxe[1] =
				oslLoadImageFile((char*) "data/Game/pickaxe.png", OSL_IN_RAM,
						OSL_PF_8888);
		Pickaxe[0] =
				oslLoadImageFile((char*) "data/Game/nopickaxe.png", OSL_IN_RAM,
						OSL_PF_8888);
		for (int i = 0; i < 2; i++)
		{
			Image[i]->centerX = Image[i]->stretchX / 2;
			Image[i]->centerY = Image[i]->stretchY / 2;
		}
		Torch =
				oslLoadImageFile((char*) "data/Game/torch.png", OSL_IN_RAM,
						OSL_PF_8888);

	}

	void Left()
	{

		if (!left)
		{
			sCamera::DelX = 240;
			sCamera::DelY = 136;
			//oslMirrorImageH(Image);
			//oslMirrorImageH(Light);
			//Image[0]->stretchX=-Image[0]->stretchX;
			//Image[1]->stretchX=-Image[1]->stretchX;
			//Torch->stretchX=-Torch->stretchX;
			left = true;
		}
		right = false;
	}
	void Right()
	{
		if (!right)
		{
			sCamera::DelX = 240;
			sCamera::DelY = 136;
			//Image[0]->stretchX=-Image[0]->stretchX;
			//Image[1]->stretchX=-Image[1]->stretchX;
			//Torch->stretchX=-Torch->stretchX;
			//oslMirrorImageH(Image);
			//oslMirrorImageH(Light);
			right = true;
		}
		left = false;
	}

	void Draw()
	{
		oslDrawImageXY((left)?Image[0]:Image[1],DrawX,DrawY);
		if (LightOn)
		{
			oslDrawImageXY(Torch,0,0);
		}
		oslDrawImageXY(Pickaxe[EnablePickaxe],400,200);

	}
	void Control()
	{
		if (LightOn){
			TorchTimer+=TorchUpdate;
			if(TorchTimer>5 || TorchTimer<0)
				TorchUpdate=-TorchUpdate;
		Torch->x-=TorchUpdate;
		Torch->y-=TorchUpdate;
		Torch->stretchX+=TorchUpdate;
		Torch->stretchY+=TorchUpdate;
		}
		if (osl_keys->pressed.circle)
		EnablePickaxe -= 1;

		if (osl_keys->pressed.left && x > 0)
		{
		if (a->GetAbility(x - 64, y) == 0)
		{
			if (left)
				x -= 64;
		}
		else
		{
			if (left && EnablePickaxe)
				a->SetAbility(x - 64, y, 1);
		}
		Left();
		}
		 //	else if(a->GetAbility(x-162,y))
		 //		a->SetAbility(x-162,y,0);

		if (osl_keys->pressed.right && x < (a->w - 1) * a->SizeTileX)
		{
		if (a->GetAbility(x + 64, y) == 0)
		{
			if (right)
				x += 64;
		}
		else
		{
			if (right && EnablePickaxe)
				a->SetAbility(x + 64, y, 1);
		}
		Right();
		}
		if (EnablePickaxe)
		{
		if (osl_keys->pressed.up && a->Ability(x, y))
		{
			a->SetAbility(x, y - 64, 1);
			if (!a->GetCrashLevel(x, y - 64))
				a->SetMap(2, x, y);

		}
		if (osl_keys->pressed.down && a->Ability(x, y))
		{
			a->SetAbility(x, y + 64, 1);
			if (!a->GetCrashLevel(x, y + 64))
				a->SetMap(2, x, y);
		}
		}

		if (!a->Ability(x, y))
		LightOn = false;
		else
		LightOn = true;
		Fall();
		}
	};

#endif /* MAN_H_ */
